//
//  CategoryViewController.m
//  Mall
//
//  Created by 孙飞燕 on 16/9/28.
//  Copyright © 2016年 孙飞燕. All rights reserved.
//

#import "CategoryViewController.h"
#import "LeftTableViewCell.h"
#import "CategoryDetailViewController.h"
#import "HPDetailCollectionViewCell.h"
@interface CategoryViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,assign)NSInteger selectedIndex;
@end

@implementation CategoryViewController

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 80, CGRectGetHeight(self.view.frame))];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 55;
        //注册cell
        [_tableView registerClass:[LeftTableViewCell class] forCellReuseIdentifier:KCellIdentifier_left];

        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(2 + 80, 2 + 64, CGRectGetWidth(self.view.frame)- 80 - 4, CGRectGetHeight(self.view.frame)- 64 - 4) collectionViewLayout:self.flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        //注册cell
        [_collectionView registerNib:[UINib nibWithNibName:@"HPDetailCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HPDetailCollectionViewCell"];
        _collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _collectionView;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.tableView];
    [self.view addSubview:self.collectionView];
}

#pragma mark --- UITableView 的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KCellIdentifier_left forIndexPath:indexPath];
    cell.nameLabel.text = @"item0";
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    [self.collectionView scrollRectToVisible:CGRectMake(0, 0, self.collectionView.frame.size.width, self.collectionView.frame.size.height) animated:YES];
    _selectedIndex = indexPath.row;
    [self.collectionView reloadData];
}

#pragma mark --- UICollectionView的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    HPDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HPDetailCollectionViewCell" forIndexPath:indexPath];
    //根据左边点击的indexPath更新右边内容
    switch (_selectedIndex) {
        case 0:
            cell.listLabel.text = @"呼吸机品牌";
            break;
        case 1:
            cell.listLabel.text = @"制氧机品牌";
            break;
        case 2:
            cell.listLabel.text = @"空气制冷剂";
            break;
        default:
            break;
    }
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((CGRectGetWidth(self.view.frame) - 80 - 4)/2, 50);
}
//点击cell进入商品浏览界面 （瀑布流）
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CategoryDetailViewController *detailVC = [[CategoryDetailViewController alloc]init];
    [self.navigationController pushViewController:detailVC animated:YES];
}
//当拖动collectionView的时候，处理tableView
//- (void)selectRowAtIndexPath:(NSInteger)index{
//    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
